package com.kgcx.flight.service.Impl;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.constant.ResultCode;
import com.kgcx.common.utils.http.HttpUtils;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.flight.domain.LZResult;
import com.kgcx.flight.service.IFlightService;
import com.kgcx.flight.utils.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

@Service
public class LZFlightService implements IFlightService {
    private static final Logger LOG = LoggerFactory.getLogger(LZFlightService.class);

    @Value("${lz_flight_api_name}")
    private String flight_api_name;
    @Value("${lz_flight_api_fdaq}")
    private String flight_api_fdaq;
    @Value("${lz_flight_app_key}")
    private String flight_app_key;
    @Value("${flight_cache_expire}")
    private Integer flight_cache_expire;

    @Autowired
    private StringRedisTemplate strRedisTemplate;

    @Override
    public ReturnData<String> QueryFlight(String fnum, String date) {
        ReturnData<String> rtn = new ReturnData<>();
        String queryKey = util.GetFlightQueryKey(fnum, date);
        SortedMap<String, Object> params = new TreeMap<String, Object>();
        params.put("appkey", flight_app_key);
        params.put("flightNo", fnum);
        params.put("date", date);
        for (String key : params.keySet()) {
            System.out.println(key + " : " + params.get(key));
        }
//        String token = HelpUtil.MD5(HelpUtil.MD5(HelpUtil.implode("&", params) + vari_flight_app_secret));

        if(strRedisTemplate.hasKey(queryKey))
        {
            String cachedResult = strRedisTemplate.opsForValue().get(queryKey);
            rtn.setCode(ResultCode.SUCCESS);
            rtn.setMsg(String.format("{%s}-航班查询成功",flight_api_name));
            rtn.setData(cachedResult);
            return rtn;
        }
        else {
            strRedisTemplate.opsForList().index(queryKey, 1L);
            String url = String.format(flight_api_fdaq + HelpUtil.implode("&", params));
            System.out.println("请求的URL：" + url);
            try {
                String result = HttpUtils.getInstance().syncGetRequest(url);
                LZResult lzResult = JsonUtil.toObject(result, LZResult.class);
                if (lzResult.getCode().equals("10000")) {
                    //成功获取，存入缓存并返回给用户
                    strRedisTemplate.opsForValue().set(queryKey, lzResult.getResult(), flight_cache_expire, TimeUnit.MINUTES);
                    rtn.setCode(ResultCode.SUCCESS);
                    rtn.setMsg(String.format("{%s}-航班查询成功", flight_api_name));
                    rtn.setData(lzResult.getResult());
                }
//            JsonUtil.toObject(result, List<FlightInfo>.getClass())
            } catch (Exception ex) {
                LOG.error("VariFlightService.queryFlight ex", ex);
                rtn.setCode(ResultCode.FAIL);
                rtn.setMsg(String.format("{%s}-航班查询异常", flight_api_name));
            }
            return rtn;
        }
    }

    @Override
    public ReturnData<String> Booking(String fnum, String date, String dep, String arr) {
        return null;
    }

    @Override
    public ReturnData<String> Listener(String content) {
        return null;
    }
}
